/**
 * Copyright (C) 2012 The SkyTvOS Project
 * <p/>
 * Version     Date           Author
 * ─────────────────────────────────────
 * 2015年3月16日         luwei
 */

package com.coocaa.app.gamecenter.framework.ui;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationSet;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

import com.coocaa.app.gamecenter.pages.arsenal.ArsenalActivity;
import com.coocaa.app.gamecenter.pages.mygame.MyGameActivity;
import com.coocaa.app.gamecenter.pages.search.GameSearchActivity;
import com.coocaa.framework.system.JoyStickManager;
import com.coocaa.framework.system.JoyStickManager.JoyStickDevice;
import com.coocaa.framework.system.JoyStickManager.OnJoyStickChanged;
import com.coocaa.x.app.framework.app.LiteAppApplication;
import com.coocaa.x.framework.app.CoocaaApplication;
import com.coocaa.x.framework.app.CoocaaApplication.NetChangeReceiver.NetChangeReceiverListener;
import com.coocaa.x.framework.utils.Android;
import com.coocaa.x.framework.utils.LogUtils;
import com.coocaa.x.uipackage.widget.SlideFocusView;
import com.tianci.appstore.R;
import com.tianci.appstore.ui.widget.TipToast;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public abstract class GameCenterBaseLayout extends SlideFocusView implements OnJoyStickChanged,
        NetChangeReceiverListener {

    protected GameCenterStatusBar mStatusBar = null;
    private Button mMyGameBtn = null;
    protected Button mSearchBtn = null;
    protected Button mArsenalBtn = null;
    private SlideFocusView.FocusViewRevision mRevisions = new FocusViewRevision(Div(35), Div(35));
    protected View mContentView = null;
    private boolean isBtnFocus = false;
    private boolean mOpenAnim = false;
    private boolean isShowArsenalButton = false;
    private boolean isInMainPage = false;
    private static final int contentAnimLen = 93;

    public GameCenterBaseLayout(Context context) {
        super(context, R.drawable.game_center_focus);
        addStatusBarLayout();
        setFocusable(false);
        setFocusableInTouchMode(false);
        JoyStickManager.registerCallback(this);
        updateHandShankIcon();
        updateNetIcon();
        CoocaaApplication.addNetChangedListener(this);
    }

    private void updateHandShankIcon() {
        List<JoyStickDevice> devices = JoyStickManager.getDeviceList();
        mStatusBar.updateHandShankPoint(devices.size());
    }

    public void setInMainPage(boolean mainpage){
        this.isInMainPage = mainpage;
    }

    private void updateNetIcon() {
        mStatusBar.updateNetPoint(Android.isNetConnected(mContext));
    }

    protected void addFuncButtonLayout() {
        LinearLayout layout = new LinearLayout(mContext);
        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(Div(280),
                LayoutParams.MATCH_PARENT);
        layout.setOrientation(LinearLayout.VERTICAL);
        layout.setPadding(0, Div(165), 0, 0);
        layout.setGravity(Gravity.CENTER_HORIZONTAL);
        addView(layout, params);
        addSearchButton(layout);
        addMyGameButton(layout);
        addArsenalButton(layout);
        mMyGameBtn.setOnKeyListener(this);
        mSearchBtn.setOnKeyListener(this);
        mArsenalBtn.setOnClickListener(this);
        mMyGameBtn.setOnClickListener(this);
        mSearchBtn.setOnClickListener(this);
        mArsenalBtn.setOnClickListener(this);
        mSearchBtn.setOnFocusChangeListener(mBtnFocusListener);
        mArsenalBtn.setOnFocusChangeListener(mBtnFocusListener);
        mMyGameBtn.setOnFocusChangeListener(mBtnFocusListener);
        mMyGameBtn.requestFocus();
        mArsenalBtn.setVisibility(GONE);
    }

    public void setBtnFocus(boolean focus) {
        mSearchBtn.setFocusable(focus);
        mSearchBtn.setFocusableInTouchMode(focus);
        mMyGameBtn.setFocusable(focus);
        mMyGameBtn.setFocusableInTouchMode(focus);
        mArsenalBtn.setFocusable(focus);
        mArsenalBtn.setFocusableInTouchMode(focus);
    }

    public void hideMyGameBtn(){
        mMyGameBtn.setVisibility(View.GONE);
    }

    public void setSearchFocus(){
        postDelayed(new Runnable() {
            @Override
            public void run() {
                mSearchBtn.requestFocus();
            }
        }, 200);
    }

    private void addStatusBarLayout() {
        mStatusBar = new GameCenterStatusBar(mContext);
        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(Div(108),
                LayoutParams.MATCH_PARENT, Gravity.RIGHT);
        mStatusBar.setBackgroundResource(R.drawable.game_center_status_bar_bg);
        addView(mStatusBar, params);
    }

    private void addSearchButton(LinearLayout layout) {
        mSearchBtn = new Button(mContext);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(Div(240), Div(134));
        params.topMargin = Div(27);
        params.leftMargin = Div(33);
        mSearchBtn.setBackgroundResource(R.drawable.game_center_search_selector);
        layout.addView(mSearchBtn, params);
    }

    private void addMyGameButton(LinearLayout layout) {
        mMyGameBtn = new Button(mContext);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(Div(240), Div(134));
        params.topMargin = Div(-50);
        params.leftMargin = Div(33);
        mMyGameBtn.setBackgroundResource(R.drawable.game_center_mygame_selector);
        layout.addView(mMyGameBtn, params);
    }

    private void addArsenalButton(LinearLayout layout) {
        mArsenalBtn = new Button(mContext);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(Div(240), Div(134));
        params.topMargin = Div(-50);
        params.leftMargin = Div(33);
        mArsenalBtn.setBackgroundResource(R.drawable.game_center_arsenal_selector);
        layout.addView(mArsenalBtn, params);
    }

    public void setMyGameFocus() {
        postDelayed(new Runnable() {
            @Override
            public void run() {
                mMyGameBtn.requestFocus();
            }
        }, 200);

    }

    public void showArsenal() {
        mArsenalBtn.setVisibility(VISIBLE);
    }
    public void openAnimation(boolean open) {
        this.mOpenAnim = open;
    }

    OnFocusChangeListener mBtnFocusListener = new OnFocusChangeListener() {

        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            LogUtils.i("luwei", v.toString() + " hasfoucus is " + hasFocus);
            if (hasFocus) {
                if (v.equals(mSearchBtn)) {
                    mMyGameBtn.setSelected(true);
                    mSearchBtn.setSelected(false);
                    mArsenalBtn.setSelected(true);
                } else if(v.equals(mMyGameBtn)){
                    mMyGameBtn.setSelected(false);
                    mSearchBtn.setSelected(true);
                    mArsenalBtn.setSelected(true);
                } else {
                    mMyGameBtn.setSelected(true);
                    mSearchBtn.setSelected(true);
                    mArsenalBtn.setSelected(false);
                }
                if (!isBtnFocus && mOpenAnim) {
                    isBtnFocus = true;
                    View view = getContentView();
                    if (view != null) {
                        transAnimation(view, 0, Div(contentAnimLen), 0, 0, mRightListener);
                    }
                    AlphaAnimation(mSearchBtn, 0.2f, 1.0f);
                    AlphaAnimation(mMyGameBtn, 0.2f, 1.0f);
                    AlphaAnimation(mArsenalBtn, 0.2f, 1.0f);
                    focusAnimation(focusView, 0, Div(contentAnimLen), 0, 0, 1.0f, 0);
                }
            } else {
                if (!mSearchBtn.isFocused() && !mMyGameBtn.isFocused() && !mArsenalBtn.isFocused()) {
                    mMyGameBtn.setSelected(false);
                    mSearchBtn.setSelected(false);
                    mArsenalBtn.setSelected(false);
                    View view = getContentView();
                    if (isBtnFocus && mOpenAnim) {
                        isBtnFocus = false;
                        if (view != null) {
                            transAnimation(view, Div(contentAnimLen), 0, 0, 0, mLeftListener);
                        }
                        AlphaAnimation(mSearchBtn, 0.2f, 1.0f);
                        AlphaAnimation(mMyGameBtn, 0.2f, 1.0f);
                        AlphaAnimation(mArsenalBtn, 0.2f, 1.0f);
                        focusAnimation(focusView, Div(contentAnimLen), 0, 0, 0, 0, 1.0f);
                    }
                }
            }
        }
    };

    private void focusAnimation(View view, int fromx, int tox, int fromy, int toy, float start,
                                float end) {
        AnimationSet set = new AnimationSet(true);
        Animation tAnimation = new TranslateAnimation(fromx, tox, fromy, toy);
        tAnimation.setFillAfter(true);
        set.addAnimation(tAnimation);
        Animation aAnimation = new AlphaAnimation(start, end);
        aAnimation.setFillAfter(true);
        set.addAnimation(aAnimation);
        set.setFillAfter(true);
        set.setDuration(300);
        view.startAnimation(set);
    }

    public void transAnimation(View view, int fromx, int tox, int fromy, int toy,
                               AnimationListener listener) {
        Animation animation = new TranslateAnimation(fromx, tox, fromy, toy);
        animation.setDuration(300);
        animation.setAnimationListener(listener);
        animation.setFillAfter(true);
        view.startAnimation(animation);
    }

    private void AlphaAnimation(View view, float start, float end) {
        Animation animation = new AlphaAnimation(start, end);
        animation.setDuration(300);
        animation.setFillAfter(true);
        view.startAnimation(animation);
    }

    private AnimationListener mRightListener = new AnimationListener() {

        @Override
        public void onAnimationStart(Animation animation) {
            // TODO Auto-generated method stub
        }

        @Override
        public void onAnimationRepeat(Animation animation) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onAnimationEnd(Animation animation) {
            // TODO Auto-generated method stub

        }
    };

    private AnimationListener mLeftListener = new AnimationListener() {

        @Override
        public void onAnimationStart(Animation animation) {
            // TODO Auto-generated method stub
        }

        @Override
        public void onAnimationRepeat(Animation animation) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onAnimationEnd(Animation animation) {
            // TODO Auto-generated method stub
        }
    };

    @Override
    public void onClick(View v) {
        String name = "";
        String id = "";
        boolean needLog = true;
        if (mSearchBtn != null && mSearchBtn.equals(v)) {
            if (LiteAppApplication.checkNetworkConnection(TipToast.showNetIsNotConnectedToastRunnable)) {
                Intent intent = new Intent(mContext, GameSearchActivity.class);
                mContext.startActivity(intent);
                if (isInMainPage)
                    name = "推荐页左侧搜索";
                else
                    name = "列表页左侧搜索";
            }else{
                needLog = false;
            }
        } else if (mMyGameBtn != null && mMyGameBtn.equals(v)) {
            Intent intent = new Intent(mContext, MyGameActivity.class);
            mContext.startActivity(intent);
            name = "我的游戏";
        } else if (mArsenalBtn != null && mArsenalBtn.equals(v)) {
            Intent intent = new Intent(mContext, ArsenalActivity.class);
            mContext.startActivity(intent);
            name = "军火库";
        }
        if (isInMainPage){
            id = "gc2_main_entry_onclick";
        }else{
            id = "gc2_second_entry_onclick";
        }
        if (needLog){
            Map<String,String> params = new HashMap<String, String>();
            params.put("entry_name",name);
            LogUtils.submit(LogUtils.PRODUCTID_APPSTORE, id, params);
        }
        super.onClick(v);
    }

    public void destory() {
        LogUtils.d("destory", getClass().getSimpleName() + " destory");
        JoyStickManager.removeCallback(this);
        CoocaaApplication.removeNetChangedListener(this);
    }

    @Override
    public void onJoyStickAttached(JoyStickDevice device) {
        // TODO Auto-generated method stub
        updateHandShankIcon();
    }

    @Override
    public void onJoyStickDeattached(JoyStickDevice device) {
        // TODO Auto-generated method stub
        updateHandShankIcon();
    }

    @Override
    public void onNetStateChanged(boolean bConnected) {
        updateNetIcon();
    }

    public abstract View getContentView();
}
